//
//  JGHomeController.m
//  JGRider
//
//  Created by 单启志 on 2018/1/25.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGHomeController.h"
#import <ZJScrollPageView/ZJScrollPageView.h>
#import "JGWorkSwitchView.h"
#import "JGRestView.h"
#import "JGRealTimeOrderController.h" //实时订单
#import "JGHaveOrderController.h"     //已接单
#import "JGMessageController.h" //消息
#import "JGUpdateWorkStatusApi.h"
#import "JGNewMessageBtn.h"
@interface JGHomeController ()<ZJScrollPageViewDelegate,JGWorkSwitchViewDelegate>

@property(nonatomic,strong)JGWorkSwitchView *switchView; //开关
@property(nonatomic,strong)JGRestView *resetView;//休息状态view
@property(nonatomic,strong)JGUpdateWorkStatusApi *api;

/**
 上一次索引
 */
@property(nonatomic,assign)NSInteger lastIndex;
@end

@implementation JGHomeController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configWorkingSubViews];

    [self.view addSubview:self.scrollPageView];
    [self.view addSubview:self.resetView];
    [self.view addSubview:self.switchView];
}

-(void)configWorkingSubViews{
    self.navigationItem.title=@"工作中";
    //sdfs
    self.automaticallyAdjustsScrollViewInsets = NO;

   JGRealTimeOrderController *realVc=[[JGRealTimeOrderController alloc]init];
    JGHaveOrderController *haveVc=[[JGHaveOrderController alloc]init];

    [self addChildViewController:realVc];
    [self addChildViewController:haveVc];
}
-(NSMutableAttributedString *)changeTitle:(NSString *)curTitle
{
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:curTitle ?: @""];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];

    [title addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:NSMakeRange(0, title.length)];

    return title;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - workSwitchView delegate
// api网络请求 修改状态
-(void)workSwitchWithStatus:(BOOL)status{

    NSString *navigationBarTitle=@"工作中";

    [self.api updateWorkStatusSuccessBlock:^(id result) {

    } faileBlock:^(id error) {

    }];

    if (status==YES) {//工作中

        [UIView animateWithDuration:0.25 animations:^{
            self.resetView.alpha=0;
            //self.scrollPageView.alpha=1;
        } completion:^(BOOL finished) {
           //TODO可能需要刷新实时订单
        }];

    }else{//休息中
        [UIView animateWithDuration:0.25 animations:^{
            self.resetView.alpha=1;
        } completion:nil];

        navigationBarTitle =@"休息中";
    }
    self.lmj_navgationBar.title=[self changeTitle:navigationBarTitle];

}
#pragma mark - ZJScrollPageView delegate
- (NSInteger)numberOfChildViewControllers {

    return self.childViewControllers.count;
}
- (UIViewController <ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {

    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    if (!childVc) {
        childVc = self.childViewControllers[index];
    }
    return childVc;
}

-(BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}

#pragma mark - 重写父类方法
//初始化 导航栏rightBtn
-(UIView *)lmjNavigationBarRightView:(LMJNavigationBar *)navigationBar{
    JGNewMessageBtn *btn=[[JGNewMessageBtn alloc]init];
    btn.count=10;
    return btn;
}
//点击事件 导航栏leftBtn
-(void)leftButtonEvent:(UIButton *)sender navigationBar:(LMJNavigationBar *)navigationBar{

    self.switchView.hidden=!self.switchView.hidden;
}
//初始化 导航栏leftBtn
-(UIImage *)lmjNavigationBarLeftButtonImage:(UIButton *)leftButton navigationBar:(LMJNavigationBar *)navigationBar{

    return [UIImage imageNamed:@"work_icon_column"];
}
//点击事件 导航栏rightBtn
-(void)rightButtonEvent:(UIButton *)sender navigationBar:(LMJNavigationBar *)navigationBar{
    //调到消息控制器
    JGMessageController *vc=[[JGMessageController alloc]init];
    JGpushViewController(vc);
}

#pragma mark - getter setter
- (ZJScrollPageView *)scrollPageView
{
    if(_scrollPageView == nil)
        {
        ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
        //显示滚动条
        style.showLine = YES;
        // 颜色渐变
        style.gradualChangeTitleColor = YES;
        style.animatedContentViewWhenTitleClicked = YES;
        style.autoAdjustTitlesWidth = YES;
        style.selectedTitleColor=MainColor;
        style.normalTitleColor=HEXCOLOR(0xb3b3b3);
        style.scrollLineColor=MainColor;
        style.segmentHeight=44;
        _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, self.lmj_navgationBar.height, JGScreenW , JGScreenH-self.lmj_navgationBar.height-44)segmentStyle:style titles:@[@"实时订单",@"已接单"] parentViewController:self delegate:self];
        _scrollPageView.backgroundColor=[UIColor clearColor];
        }
    return _scrollPageView;
}

-(JGWorkSwitchView *)switchView{

    if (_switchView==nil) {
        _switchView=[[JGWorkSwitchView alloc]initWithFrame:CGRectMake(0, self.lmj_navgationBar.height, JGScreenW, JGScreenH-self.lmj_navgationBar.height)];
        _switchView.hidden=YES;
        _switchView.delegate=self;
    }
    return _switchView;
}

-(JGRestView *)resetView{
    if (_resetView==nil) {
        _resetView=[[JGRestView alloc]initWithFrame:CGRectMake(0, self.lmj_navgationBar.height, JGScreenW, JGScreenH-self.lmj_navgationBar.height)];
        _resetView.alpha=0;
    }
    return _resetView;
}

-(JGUpdateWorkStatusApi *)api{
    if (!_api) {
        _api=[[JGUpdateWorkStatusApi alloc]init];
    }
    return _api;
}

@end
